package com.actions;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.bean.Craft;
import com.bean.CraftGrade;
import com.bean.CraftShow;
import com.bean.CraftType;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.serviceImpl.CraftGradeServiceImpl;
import com.serviceImpl.CraftServiceImpl;
import com.serviceImpl.CraftTypeServiceImpl;
import com.serviceImpl.UtilSupport;


public class CraftAction extends ActionSupport {

	private UtilSupport util;	//公用类
	private CraftServiceImpl craftBiz;	//工艺serviceimpl类
	private CraftGradeServiceImpl craftGradeBiz;//工艺等级serviceimpl类
	private CraftTypeServiceImpl craftTypeBiz;//工艺类型serviceimpl类
	
	
	//集合
	private List<CraftShow> allCraft;	//接收所有的工艺
	private List<CraftGrade> allCraftGrade;	//接收所有的工艺等级
	private List<CraftType> allCraftType;	//接收所有的工艺类型
	private Map<String, Object> jsonResult;
	private JSONArray craftGrade;
	
	//实体类
	private CraftShow craftShow;//工艺显示类
	private Craft craft;//工艺类
	
	private boolean Flag;//判断时候成功
	
	//查询字段
	private int offset;			//当前页
	private int pageSize=10;		//每页显示几条
	private int totalcount;		// 总记录数
	private int totalpage; 		// 总页数
	
	//查询
	private String craftCode;	//工艺编码
	private String craftName;	//工艺名称
	private Double stdPrice;	//标准价格
	private int craftTypeId;	//工艺类型id
	private int craftId;	//工艺id
	
	private String refreshList;//封装刷新的页面
	private String titleName;//封装title名字
	private String msg;//封装jsp提示语
	
	//封装
	public UtilSupport getUtil() {
		return util;
	}

	public int getCraftTypeId() {
		return craftTypeId;
	}

	public void setCraftTypeId(int craftTypeId) {
		this.craftTypeId = craftTypeId;
	}

	public CraftShow getCraftShow() {
		return craftShow;
	}

	public void setCraftShow(CraftShow craftShow) {
		this.craftShow = craftShow;
	}

	public Craft getCraft() {
		return craft;
	}

	public void setCraft(Craft craft) {
		this.craft = craft;
	}

	public void setUtil(UtilSupport util) {
		this.util = util;
	}

	public CraftServiceImpl getCraftBiz() {
		return craftBiz;
	}

	public void setCraftBiz(CraftServiceImpl craftBiz) {
		this.craftBiz = craftBiz;
	}

	public List<CraftShow> getAllCraft() {
		return allCraft;
	}

	public void setAllCraft(List<CraftShow> allCraft) {
		this.allCraft = allCraft;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalcount() {
		return totalcount;
	}

	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}

	public String getCraftCode() {
		return craftCode;
	}

	public void setCraftCode(String craftCode) {
		this.craftCode = craftCode;
	}

	public String getCraftName() {
		return craftName;
	}

	public void setCraftName(String craftName) {
		this.craftName = craftName;
	}
	
	public Double getStdPrice() {
		return stdPrice;
	}

	public void setStdPrice(Double stdPrice) {
		this.stdPrice = stdPrice;
	}

	public CraftGradeServiceImpl getCraftGradeBiz() {
		return craftGradeBiz;
	}

	public void setCraftGradeBiz(CraftGradeServiceImpl craftGradeBiz) {
		this.craftGradeBiz = craftGradeBiz;
	}

	public CraftTypeServiceImpl getCraftTypeBiz() {
		return craftTypeBiz;
	}

	public void setCraftTypeBiz(CraftTypeServiceImpl craftTypeBiz) {
		this.craftTypeBiz = craftTypeBiz;
	}

	public List<CraftGrade> getAllCraftGrade() {
		return allCraftGrade;
	}

	public void setAllCraftGrade(List<CraftGrade> allCraftGrade) {
		this.allCraftGrade = allCraftGrade;
	}

	public List<CraftType> getAllCraftType() {
		return allCraftType;
	}

	public void setAllCraftType(List<CraftType> allCraftType) {
		this.allCraftType = allCraftType;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	public JSONArray getCraftGrade() {
		return craftGrade;
	}

	public void setCraftGrade(JSONArray craftGrade) {
		this.craftGrade = craftGrade;
	}
	
	public String getRefreshList() {
		return refreshList;
	}

	public void setRefreshList(String refreshList) {
		this.refreshList = refreshList;
	}

	public String getTitleName() {
		return titleName;
	}

	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getCraftId() {
		return craftId;
	}

	public void setCraftId(int craftId) {
		this.craftId = craftId;
	}
	

	public boolean isFlag() {
		return Flag;
	}

	public void setFlag(boolean flag) {
		Flag = flag;
	}

	
	/**
	 * 获取所有工艺信息
	 * @return
	 */
	public String craftShowList(){
		getCrafts();
		return "show";
	}
	
	/**
	 * 选择工艺
	 * @return
	 */
	public String chooseCrafts(){
		getCrafts();
		return "chooseCraft";
	}
	
	private void getCrafts(){
		//获取工艺等级
		allCraftType=craftTypeBiz.getAllCraftType();
		//获取工艺类型
		allCraftGrade=craftGradeBiz.getAllCraftType();

		HttpServletRequest request = ServletActionContext.getRequest();
		StringBuffer sqlbf = new StringBuffer("select count(*) from audit.craft WHERE 0=0 ");

		String	codeSql = "";
		String nameSql = "";
		String priceSql = "";

		try {
			this.craftCode = request.getParameter("code");
			if(craftCode!=null&&!craftCode.isEmpty()){
				craftCode=new String(craftCode.trim().getBytes("ISO-8859-1"),"UTF-8");

				codeSql = "and craft_code like '%"+craftCode+"%'";

				sqlbf.append(codeSql);

			}

			this.craftName = request.getParameter("name");
			if(craftName!=null&&!craftName.isEmpty()){
				craftName=new String(craftName.trim().getBytes("ISO-8859-1"),"UTF-8");

				nameSql="and craft_name = '"+craftName+"'";

				sqlbf.append(nameSql);
			}

			String price = request.getParameter("stdPrice");
			if(price!=null&&!price.isEmpty()){
				this.stdPrice = (double) Integer.parseInt(price);

				priceSql="and std_price = "+stdPrice+"";

				sqlbf.append(priceSql);
			}



			String sql =sqlbf.toString();

			totalcount = util.getTotalCount(sql);

			totalpage = totalcount % pageSize == 0 ? totalcount / pageSize
					: totalcount / pageSize + 1;

			offset = getPageOffset();
			//表名
			String table="v_craft";
			//主键
			String pk="craft_id";
			//排序字段
			String sort="craft_id";
			//当前页数
			int PageNumber=offset+1;

			int PageSize=pageSize;

			//查询条件
			String options=(codeSql+nameSql+priceSql)==null?"":"0=0"+codeSql+nameSql+priceSql;
			allCraft=craftBiz.getAllCraft(table, pk, sort, PageNumber, PageSize, options);

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	// Added by JSL : 获取翻页偏移量(实际上是将要翻到的页面的页索引，页索引从 0 开始)
	private int getPageOffset() {
		HttpServletRequest request=ServletActionContext.getRequest();
		String ofst = request.getParameter("offset");
		int idx = 0;
		if(ofst!=null){
			idx = Integer.valueOf(ofst);
			idx = idx < 0 ? 0 : idx;                        // 超过第一页时，不再翻页
			idx = idx >= totalpage ? (totalpage-1) : idx;	// 超过最后一页时，不再翻页		
		}
		return idx;
	}

	
	/**
	 *点击新增页面加载下拉框的数据 
	 */
	public String addCraftJsp(){
		
		//接收所有的工艺
	   allCraftType=craftTypeBiz.getAllCraftType();
		//接收所有的工艺等级
	   allCraftGrade=	craftGradeBiz.getAllCraftType();
		
		return "addCraft";
	}
	
	/**
	 * 点击类型获取工艺编码
	 */
	public String createCraftCode(){
		jsonResult = new HashMap<String, Object>();
		//接收传过来的工艺类型
		HttpServletRequest request = ServletActionContext.getRequest();
		String TypeId = request.getParameter("craftTypeId");
		int craftTypeId = Integer.valueOf(TypeId);
		
		String craftCode = craftBiz.getCrafCodeByType(craftTypeId);
		jsonResult.put("craftCode", craftCode);
		return "code";
	}
	
	/**
	 * 获取工资
	 */
	public String wageUnit(){
		//接收所有的工艺等级
		allCraftGrade=	craftGradeBiz.getAllCraftType();
		
		craftGrade=JSONArray.fromObject(allCraftGrade); 
		
		return "wu";
	}
	
	/**
	 * 添加工艺
	 */
	public String addcraft(){
		HttpServletRequest request = ServletActionContext.getRequest();
			
		craftBiz.saveCraft(craft);
		
		HttpSession session = request.getSession(false);
		
		String craftName = craft.getCraftName();
		
		refreshList = "cracraftShowList";
		titleName = "面料工艺";
		msg = "工艺 【 "+craftName+" 】 ";
		
		return "add";
	}	
	
	
	/**
	 * 通过id获取相应的工艺信息
	 */
	public String findCraft(){
		HttpServletRequest request=ServletActionContext.getRequest();
		
		//接收工艺id
		String  id= request.getParameter("craftId");
		craftId=Integer.parseInt(id);
	
		//调用查找方法
		craftShow=craftBiz.getByidCraft(craftId);
		
		//接收所有的工艺等级
		allCraftGrade=	craftGradeBiz.getAllCraftType();
		return "find";
	}
	
	/**
	 * 修改工艺信息
	 */
	public String amendCraft(){
		
		craftBiz.mergeCraft(craft);
		
		return "merge";
	}
	
	
	/**
	 * 删除工艺信息
	 */
	public String delCraft() {
		HttpServletRequest request=ServletActionContext.getRequest();
		
		//接收工艺id
		String  id= request.getParameter("craftId");
		craftId=Integer.parseInt(id);
		
		Flag=craftBiz.delByCraft(craftId);
		
		return "del";
	}
}
